package com.xuanl.protagonist.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuanl.protagonist.dto.ProtagonistUserDo;
import com.xuanl.protagonist.mapper.ProtagonistUserMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import protagonist.common.util.MD5Util;
import protagonist.exception.LlException;

import javax.annotation.Resource;
import java.util.Objects;

@Service
@Slf4j
public class ProtagonistUserServiceImpl implements ProtagonistUserService {

    @Resource
    private ProtagonistUserMapper protagonistUserMapper;

    @SneakyThrows
    @Override
    public ProtagonistUserDo login(String userName,String password) {

        ProtagonistUserDo protagonistUserDo = protagonistUserMapper
                .selectOne(new LambdaQueryWrapper<ProtagonistUserDo>().eq(ProtagonistUserDo::getUserName,userName));
        if(Objects.isNull(protagonistUserDo)){
            throw new LlException("用户不存在");
        }
        if(!MD5Util.validateString(protagonistUserDo.getPassword(),password)){
            throw new LlException("密码错误");
        }
        return protagonistUserDo;
    }

    @Override
    public ProtagonistUserDo test() {
        return null;
    }

}
